package com.nowcoder.community.community.entity;


/**
 * 封装分页相关的信息。
 *
 */
public class Page {

//    当前页码
    private int currentPage = 1;

//   显示的上限；
    private int limit = 10;

//    数据总数（用于计算总页数）
    private int rows;

//    查询路径
    private String path;

    public int getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(int currentPage) {
        if(currentPage >= 1) {
            this.currentPage = currentPage;
        }
    }

    public int getLimit() {
        return limit;
    }

    public void setLimit(int limit) {
        if(limit >= 1 && limit <= 100){
            this.limit = limit;
        }
    }

    public int getRows() {
        return rows;
    }

    public void setRows(int rows) {
        if(rows >= 0){
            this.rows = rows;
        }
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    /**
     * 获取当前页的起始行
     * @return
     */
    public int getOffset(){
        int total = ((getTotal()-1) >= 0 ? (getTotal()-1) : 0) *limit;
        int now = currentPage*limit - limit;
        return now > total ? total : now;
    }

    /**
     * 获取总的页数
     * @return
     */
    public int getTotal(){
        if(rows%limit == 0){
            return rows/limit;
        }
        return rows/limit + 1;
    }


    /**
     * 获取起始页码
     */

    public int getFrom(){
        int from = currentPage - 2;
        int total = getTotal();
//        防止页码输入超过范围导致显示错误
        if(from < 1){
            from = 1;
        }else if(from > total - 2){
            from = total-2;
        }
        return from;
    }

    /**
     * 获取结束页
     * @return
     */
    public int getTo(){
        int to = currentPage + 2;
        int total = getTotal();
        return to > total ? total:to;
    }
}
